<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
define("PRIMARY_KEY","user_id");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_tools_userman.php";
$FileTemplate  = "ais_tools_userman_list.html";
$TableName     = "$DBGeneral.tbluser";
$SubHeaderTitle= "User Management";
$PageSize      = 10 ;
$listCheck     = 50 ;
$HTMLGridList  = "ToolGrid";
//************ FORM ACTION **************

    if (GetParam("Action","")=="Delete") {		
        DeleteRecord("txtCheck",$listCheck);
    }

//Initiate Database Table For Search;
$FUser = GetParam("userSearch","");
$FGroup = GetParam("groupSearch","");

//******* DATABASE FORM QUERY ***********

$SQL = "SELECT $TableName.*,$DBGeneral.tblstaff.nama,$DBApp.tblgroup.group_name FROM $TableName
              LEFT JOIN $DBGeneral.tblstaff ON $TableName.user_id=$DBGeneral.tblstaff.user_id
                LEFT JOIN $DBApp.tblusergroup ON $DBApp.tblusergroup.user_id=$TableName.user_id
                LEFT JOIN $DBApp.tblgroup ON $DBApp.tblusergroup.group_id = $DBApp.tblgroup.group_id
                 ";


$SQLCount = "SELECT count(*) FROM $TableName,
                         $DBApp.tblusergroup,
                         $DBApp.tblgroup,
                         $DBGeneral.tblstaff ";

//Retrieve Parameter from search

if ($FUser) {
    $CustomWhere = " user like '".$FUser."%'";
    if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
    else $WHERE = $CustomWhere;
}

if ($FGroup) {
    $CustomWhere = " tblusergroup.group_id = '".$FGroup."'";
    if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
    else $WHERE = $CustomWhere;
}

//Build  WHERE
if (strlen($WHERE)) $WHERE = " WHERE ".$WHERE;
else $WHERE = " WHERE 1";


$SQL = $SQL.$WHERE;
//print $SQL;
$SQLCount = $SQLCount.$WHERE;

//Select User Group
//$SQLGroup = "SELECT * FROM idms_ais.tblgroup ORDER BY group_name ";
$SQLGroup = "SELECT * FROM tblgroup ORDER BY group_name ";

//******* SHOW FORM CONTENT *********
//Default value
if (!isset($_GET)) $_GET=&$HTTP_GET_VARS ;
if (isset($_GET['PageNum'])) {
  $PageNum = $_GET['PageNum'] ;
} else {
    $PageNum = 1 ;
}

//Default value
if (isset($_GET['RecCnt'])) {
  $RecCnt = intval($_GET['RecCnt']) ;
} else {
    $RecCnt = -1 ;
}

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$RecCnt = $TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
$TBS->MergeBlock("groupblk",$DBMysql,$SQLGroup);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ;
$TBS->Show();


function DeleteRecord($sequence_name,$listCheck) {
    global $DBConnection,$FileName,$TableName;
    
    for ($x=1;$x<=$listCheck;$x++) {				
        if (GetParam($sequence_name.$x,"")) {			
            $DBConnection->delete("$TableName",$sequence_name.$x);
        }
    }
    
    header("Location: $FileName");
    exit;

}

?>
